﻿#region

using System;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Command;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Versioning;
using Rio.Framework.Localisation;

#endregion

namespace Rio.Framework.Pages.Controls
{
    public partial class VersionInformations
    {
        #region Delegate

        public delegate void VersionSelectedHandler(object sender, VersionSelectedEventArgs args);

        #endregion

        #region Attributs

        #region Readonly & Static Fields

        public static readonly DependencyProperty DocumentProperty =
            DependencyProperty.Register("CurrentDocument", typeof (Document), typeof (VersionInformations), new PropertyMetadata(null, OnDocumentChanged));

        public static readonly DependencyProperty SelectedVersionProperty =
            DependencyProperty.Register("SelectedVersion", typeof (VersioningItem), typeof (VersionInformations), new PropertyMetadata(null, OnVersionChanged));

        private readonly RelayCommand<object> _addVersionCommand;
        private readonly RelayCommand<object> _checkOutCommand;
        private readonly RelayCommand<object> _makeToMajorCommand;
        private readonly RelayCommand<object> _viewSpecificVersionCommand;

        #endregion

        #region Fields

        private string _textVersionHistory = string.Empty;
        private string _versionCreatedByOn = string.Empty;
        private string _versionNumber = string.Empty;

        #endregion

        #endregion

        #region Properties

        #region Instance Properties

        /// <summary>
        ///   Gets the add version command.
        /// </summary>
        /// <value> The add version command. </value>
        public RelayCommand<object> AddVersionCommand
        {
            get
            {
                return _addVersionCommand;
            }
        }

        /// <summary>
        ///   Gets the make to major command.
        /// </summary>
        /// <value> The make to major command. </value>
        public RelayCommand<object> CheckOutCommand
        {
            get
            {
                return _checkOutCommand;
            }
        }


        /// <summary>
        ///   Gets or sets the document.
        /// </summary>
        /// <value> The document. </value>
        public Document CurrentDocument
        {
            get
            {
                return (Document) GetValue(DocumentProperty);
            }
            set
            {
                SetValue(DocumentProperty, value);
            }
        }

        /// <summary>
        ///   Gets the make to major command.
        /// </summary>
        /// <value> The make to major command. </value>
        public RelayCommand<object> MakeToMajorCommand
        {
            get
            {
                return _makeToMajorCommand;
            }
        }

        /// <summary>
        ///   Gets or sets the selected version.
        /// </summary>
        /// <value> The selected version. </value>
        public VersioningItem SelectedVersion
        {
            get
            {
                return (VersioningItem) GetValue(SelectedVersionProperty);
            }
            set
            {
                SetValue(SelectedVersionProperty, value);
            }
        }

        /// <summary>
        ///   Text history for selected versioning
        /// </summary>
        public string TextVersionHistory
        {
            get
            {
                return _textVersionHistory;
            }
            set
            {
                _textVersionHistory = value;
                OnPropertyChanged("TextVersionHistory");
            }
        }

        /// <summary>
        ///   Gets the view specific version command.
        /// </summary>
        /// <value> The view specific version command. </value>
        public RelayCommand<object> ViewSpecificVersionCommand
        {
            get
            {
                return _viewSpecificVersionCommand;
            }
        }

        #endregion

        #region Instance Methods

        /// <summary>
        ///   Clears the current version.
        /// </summary>
        public void ClearVersion()
        {
            SelectedVersion = null;
            InitializeVersionInformation();
        }

        #endregion

        #endregion

        #region Events

        #region Instance Methods

        private void RaiseAddVersion()
        {
            if (AddVersion != null)
                AddVersion(this, new RoutedEventArgs());
        }

        private void RaiseCheckOut()
        {
            if (CheckOut != null)
                CheckOut(this, new VersionSelectedEventArgs(SelectedVersion));
        }

        private void RaiseMakeToMajor()
        {
            if (MakeToMajor != null)
                MakeToMajor(this, new VersionSelectedEventArgs(SelectedVersion));
        }

        private void RaiseVersionSelected()
        {
            if (VersionSelected != null)
                VersionSelected(this, new VersionSelectedEventArgs(SelectedVersion));
        }

        #endregion

        #region Event Declarations

        public event RoutedEventHandler AddVersion;
        public event VersionSelectedHandler CheckOut;
        public event VersionSelectedHandler MakeToMajor;
        public event VersionSelectedHandler VersionSelected;

        #endregion

        #endregion

        #region Constructors

        /// <summary>
        ///   Initializes a new instance of the <see cref="VersionInformations" /> class.
        /// </summary>
        public VersionInformations()
        {
            InitializeComponent();

            InitLabel();

            TextVersionHistory = string.Empty;

            _addVersionCommand = new RelayCommand<object>(OnAddVersion, CanExecuteAddVersionCommand);
            _makeToMajorCommand = new RelayCommand<object>(OnMakeToMajor, CanExecuteMakeMajorCommand);
            _checkOutCommand = new RelayCommand<object>(OnCheckOut, CanExecuteAddVersionCommand);
            _viewSpecificVersionCommand = new RelayCommand<object>(OnViewSpecificVersion);
        }

        #endregion

        #region Functions

        #region Instance Methods

        /// <summary>
        ///   Must be override in child class
        /// </summary>
        public override sealed void InitLabel()
        {
            base.InitLabel();

            AvalaibleVersions.Text = Resx.GetLabel(ResxFolderPage.AVALAIBLE_VERSIONS);
            TextBlockAddVersion.Text = Resx.GetLabel(ResxFolderPage.ADD_NEW_VERSION);
            VersionHistoryTextBlock.Text = Resx.GetLabel(ResxFolderPage.VERSION_HISTORIC);
            TextBlockMakeToMajor.Text = Resx.GetLabel(ResxFolderPage.MAKE_MAJOR_VERSION);

            _versionCreatedByOn = Resx.GetLabel(ResxFolderPage.VERSION_CREATEDBY_ON);
            _versionNumber = Resx.GetLabel(ResxFolderPage.VERSION_NUMBER);
        }

        /// <summary>
        ///   Disables the information.
        /// </summary>
        public void DisableInformation()
        {
            VisualStateManager.GoToState(this, "NoData", false);
        }

        /// <summary>
        ///   Enableds the information.
        /// </summary>
        public void EnabledInformation()
        {
            VisualStateManager.GoToState(this, "Default", false);
        }

        /// <summary>
        ///   Initializes the version information.
        /// </summary>
        public void InitializeVersionInformation()
        {
            //Uncomment: 5.
            if (SelectedVersion != null && !SelectedVersion.IsOriginalVersion)
            {
                TextVersionHistory = string.Format(_versionNumber, SelectedVersion.Major, SelectedVersion.Minor);
                TextVersionHistory += Environment.NewLine + Environment.NewLine + string.Format(_versionCreatedByOn, SelectedVersion.CreatedOn, SelectedVersion.CreatedBy);
            }
            else
            {
                TextVersionHistory = string.Empty;
            }

            MakeToMajorCommand.RaiseCanExecuteChanged();
        }

        /// <summary>
        ///   Determines whether this instance [can execute add version command] the specified parameter.
        /// </summary>
        /// <param name="parameter"> The parameter. </param>
        /// <returns> </returns>
        private bool CanExecuteAddVersionCommand(object parameter)
        {
            if (CurrentDocument != null && CurrentDocument.IsVersionable)
                return true;
            return false;
        }

        /// <summary>
        ///   Determines whether this instance [can execute make major command] the specified parameter.
        /// </summary>
        /// <param name="parameter"> The parameter. </param>
        /// <returns> </returns>
        private bool CanExecuteMakeMajorCommand(object parameter)
        {
            if (SelectedVersion != null && SelectedVersion.Minor > 0)
                return true;
            return false;
        }

        /// <summary>
        ///   Inits the document.
        /// </summary>
        private void InitializeDocumentInformations()
        {
            if (CurrentDocument != null)
            {
                EnabledInformation();
            }
            else
            {
                DisableInformation();
            }
        }

        /// <summary>
        ///   Called when [add version].
        /// </summary>
        /// <param name="parameter"> The parameter. </param>
        private void OnAddVersion(object parameter)
        {
            RaiseAddVersion();
        }

        /// <summary>
        ///   Called when [make to major].
        /// </summary>
        /// <param name="parameter"> The parameter. </param>
        private void OnCheckOut(object parameter)
        {
            RaiseCheckOut();
        }

        /// <summary>
        ///   Called when [make to major].
        /// </summary>
        /// <param name="parameter"> The parameter. </param>
        private void OnMakeToMajor(object parameter)
        {
            RaiseMakeToMajor();
        }

        /// <summary>
        ///   Called when [view specific version].
        /// </summary>
        /// <param name="parameter"> The parameter. </param>
        private void OnViewSpecificVersion(object parameter)
        {
            SelectedVersion = parameter as VersioningItem;

            InitializeVersionInformation();

            RaiseVersionSelected();
        }

        #endregion

        #region Class Methods

        /// <summary>
        ///   Called when [document changed].
        /// </summary>
        /// <param name="source"> The source. </param>
        /// <param name="e"> The <see cref="System.Windows.DependencyPropertyChangedEventArgs" /> instance containing the event data. </param>
        public static void OnDocumentChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            VersionInformations context = source as VersionInformations;
            if (context != null) context.InitializeDocumentInformations();
        }


        /// <summary>
        ///   Called when [version changed].
        /// </summary>
        /// <param name="source"> The source. </param>
        /// <param name="e"> The <see cref="System.Windows.DependencyPropertyChangedEventArgs" /> instance containing the event data. </param>
        public static void OnVersionChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            VersionInformations context = source as VersionInformations;
            if (context != null) context.InitializeVersionInformation();
        }

        #endregion

        #endregion

        #region Internal Types

        public class VersionSelectedEventArgs
            : RoutedEventArgs
        {
            #region Constructors

            /// <summary>
            ///   Initializes a new instance of the <see cref="VersionSelectedEventArgs" /> class.
            /// </summary>
            /// <param name="selectedVersion"> The selected version. </param>
            public VersionSelectedEventArgs(VersioningItem selectedVersion)
            {
                SelectedVersion = selectedVersion;
            }

            #endregion

            #region Instance Properties

            /// <summary>
            ///   Gets or sets the selected version.
            /// </summary>
            /// <value> The selected version. </value>
            public VersioningItem SelectedVersion { get; private set; }

            #endregion
        }

        #endregion

        #region Event Handling

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedVersion = listBox.SelectedItem as VersioningItem;

            InitializeVersionInformation();

            RaiseVersionSelected();
        }

        #endregion
    }
}